// app.js
var util = require('./utils/util.js')
let shangjiOpenId = null
App({
  globalData: {
    isRegistered: null,
    openId: null,
    userInfo: null,
    pingtai_tag: '曲靖找房',
    SysController:null,
    gotoAppid:{}
  },
  onShow(options) {
    //this.getFangwen() // 访问次数方法
  },
  onLaunch(options) {
    // 初始化云开发环境
    wx.cloud.init({
      env: 'cloud1-8gsr6rqv78f09fb8'
    })
    // 获取全局控制变量
    this.getController()
    // 接收分享好友传参
    if (options.query.shangjiOpenId) {
      shangjiOpenId = options.query.shangjiOpenId
    }
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage']
    })
    // 获取愤豆网的跳转小程序appid
    this.getGotoAppid()
  },
  // https://www.fendou.life/api/common/appid
  getGotoAppid(){
    wx.request({
      url: 'https://www.fendou.life/api/common/appid',
      success:res=>{
        this.globalData.gotoAppid = {
          appid:res.data[0].appid,
          name:res.data[0].name
        }
        // console.log(this.globalData.gotoAppid,'===========');
      }
    })
  },

  // 异步获取全局控制变量
  getController(){
    return new Promise((resolve,reject)=>{
      wx.cloud.database().collection('controller').doc('cd045e7561461f470f3d6e035d201be1').get().then(res=>{
        this.globalData.SysController = res.data
        resolve(res.data)
        // console.log(this.globalData.SysController);
      })
    })
  },
  // 访问次数方法
  getFangwen() {
    wx.cloud.callFunction({
      name: 'fangwen',
      data: {
        tag: 'fangwencishu',
        _updateTime: util.formatTime(new Date()).slice(0, 10)
      }
    }).then(()=>{
    }).catch(()=>{
    })
  },
  // 查询用户状态
  selecteUser() {
    return new Promise((resolve, reject) => {
      wx.cloud.callFunction({
          name: 'user',
          data: {
            tag: 'selecteUser',
            _updateTime: util.formatTime(new Date()).slice(0, 10)
          }
        })
        .then(res => {
          if (res.result.length === 1) {
            // 1.用户已经注册，判断禁用状态  为true 则保存注册状态  为false 则叫他滚蛋
            if (res.result[0].status) {
              this.globalData.userInfo = res.result[0]
              resolve(true)
            } else {
              resolve(false)
              this.jingyongShow()
            }
          } else {
            // 直接注册
            resolve(this.getUserProfileModel())
          }
        })
    })
  },
  // 授权弹窗
  getUserProfileModel() {
    // 获取授权
    return new Promise((resolve, reject) => {
      wx.showModal({
        title: '提示',
        content: '授权后才可以继续操作',
        success: res => {
          if (res.confirm) {
            this.getUserProfile().then(res => {
              resolve(res)
            })
          } else {
            resolve(false)
          }
        }
      })
    })
  },
  // 获取登录授权
  getUserProfile() {
    return new Promise((resolve, reject) => {
      wx.getUserProfile({
        desc: '用于完善资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          this.globalData.userInfo = res.userInfo
          this.zhuceUser(res.userInfo).then(res => {
            resolve(res)
          })
        },
        fail: (res) => {
          resolve(false)

        }
      })
    })

  },
  // 注册用户
  zhuceUser(userInfo) {
    return new Promise((resolve, reject) => {
      wx.cloud.callFunction({
          name: 'user',
          data: {
            tag: 'zhuceUser',
            avatarUrl: userInfo.avatarUrl,
            city: userInfo.city,
            gender: userInfo.gender,
            nickName: userInfo.nickName,
            province: userInfo.province,
            shangjiOpenId: shangjiOpenId,
            createTime: util.formatTime(new Date()),
            _updateTime: util.formatTime(new Date()).slice(0, 10),
            jinbiNum_zhuce: this.globalData.SysController.jinbi_zhi.zhuce
          }
        })
        .then(res => {
          this.globalData.userInfo._openid = res.result
          wx.showToast({
            icon: 'none',
            title: `积分+${this.globalData.SysController.jinbi_zhi.zhuce}`
          })
          // 给上级积分加10
          if (shangjiOpenId) {
            this.addJinbi(this.globalData.SysController.jinbi_zhi.yaoqing, shangjiOpenId)
          }
          resolve(true)
        })
    })
  },
  // 加积分操作
  addJinbi(zhi, mubiao) {
    wx.cloud.callFunction({
      name: 'user',
      data: {
        tag: 'addJinbi',
        zhi: zhi,
        mubiao: mubiao
      }
    })
  },

  // 定义检查用户禁用状态
  jingyongShow() {
    wx.showModal({
      showCancel: false,
      title: '账号禁用提示',
      content: '请联系管理员处理！',
      cancelColor: 'cancelColor',
      complete: () => {
        wx.exitMiniProgram()
      }
    })
  },

})